#!/usr/bin/env python3
# __*__coding:utf-8__*__

def sql_analyze(sql):
    '''
    SQL解析工作
    :param sql:
    :return:
    '''

    analyze_func = {
        'insert':insert_analyze,
        'delete':delete_analyze,
        'update':update_analyze,
        'select':select_analyze,
    }
    sql_l = sql.split(' ')
    func = sql_l[0]
    result = []
    if func in analyze_func:
        result = analyze_func[func](sql_l)
        # print(analyze_func[func](sql_l))
    return result

def insert_analyze(sql_l):
    pass

def delete_analyze(sql_l):
    pass

def update_analyze(sql_l):
    pass

def select_analyze(sql_l):
    print('from in the select_analyze %s' %sql_l)
    sql_dict = {
        'func':select,
        'select':[],
        'from':[],
        'where':[],
    }
    return run_analyze(sql_l,sql_dict)

def run_analyze(sql_l,sql_dict):
    print('%s %s' %(sql_l,sql_dict))
    tag = False
    for item in sql_l:
        if tag and item in sql_dict:
            tag = False
        if not tag and item in sql_dict:
            tag = True
            key = item
            continue
        if tag:
            sql_dict[key].addend(item)
    if sql_dict.get('where'):
        sql_dict['where'] = where_analyze(sql_dict.get('where'))
    return sql_dict

def where_analyze(where_l):
    result=[]
    key=['and','or','not']
    char=''
    for i in where_l:
        if len(i) == 0: continue
        if i in key:
            # 判断i是否为key当中的逻辑运算符
            if len(char) != 0:
                result.append(char)
                result.append(i)
                char = ''
        else:
            char += i   # 'id>4'
    else:
        result.append(char)
    print(result)
    return result

def sql_action(sql_dict):
    '''

    :param sql_dict:
    :return:
    '''
    # print(sql_dict.get('func')(sql_dict))
    pass

def insert_action(sql_dict):
    pass

def delete_action(sql_dict):
    pass

def update_action(sql_dict):
    pass

def select_action(sql_dict):
    pass

# select name,age from staff_table where age > 22
# select  * from staff_table where dept = "IT"
# select  * from staff_table where enroll_date like "2013"

while True:
    sql=input("sql>> ").strip()
    if sql == 'exit':exit()
    if len(sql) == 0:continue

    sql_dict=sql_analyze(sql)

    if len(sql_dict) == 0:continue
    result=sql_action(sql_dict)






























